//
//  NBANetworking.m
//  NBA
//
//  Created by Jaedong on 2018/3/16.
//  Copyright © 2018年 Jaedong. All rights reserved.
//

#import "NBANetworking.h"
#import <AFNetworking.h>
#import "NBAMatch.h"
#import <YYModel.h>

@implementation NBANetworking

+ (instancetype)shareInstance {
    static dispatch_once_t token;
    static NBANetworking *networking;
    dispatch_once(&token, ^{
        networking = [NBANetworking new];
    });
    return networking;
}

- (void)loadTodayMatchsWith:(CompletionBlock)completionBlock error:(ErrorBlock)errorBlock {
    NSMutableArray *matchs = [NSMutableArray new];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:@"http://china.nba.com/static/data/scores/miniscoreboardlive.json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *matchsArray = responseObject[@"payload"][@"today"][@"games"]; // 拿到今日比赛的数据
        for (NSDictionary *matchDic in matchsArray) { // 比赛数据
            NBAMatch *match = [NBAMatch yy_modelWithJSON:matchDic];
            [matchs addObject:match];
        }
        completionBlock(matchs);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorBlock(@"网络请求失败！");
    }];
}

@end
